package com.fuqi.android.zhongcai.base.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.sql.Worker;

import java.util.List;

/**
 * Created by juner on 2016/7/21.
 */
public class SearchResultAdapter extends BaseAdapter {
    private Context mContext;
    private List<Worker> mWorkers;

    public SearchResultAdapter(Context mContext, List<Worker> mWorkers) {
        this.mWorkers = mWorkers;
        this.mContext = mContext;
    }

    public void changeData(List<Worker> list){
        if (mWorkers == null){
            mWorkers = list;
        }else{
            mWorkers.clear();
            mWorkers.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mWorkers == null ? 0 : mWorkers.size();
    }

    @Override
    public Worker getItem(int position) {
        return mWorkers == null ? null : mWorkers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ResultViewHolder holder;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.item_search_result_listview, parent, false);
            holder = new ResultViewHolder();
            holder.name = (TextView) view.findViewById(R.id.tv_item_result_listview_name);
            view.setTag(holder);
        }else{
            holder = (ResultViewHolder) view.getTag();
        }
        holder.name.setText(mWorkers.get(position).getName());
        return view;
    }

    public static class ResultViewHolder{
        TextView name;
    }
}
